package com.ce.pms.charge.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

import javax.servlet.http.HttpServletResponse;

import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.XMLOutputter;

import com.ce.pms.base.web.PmsBaseAction;
import com.ce.pms.charge.service.IBusinessCodeService;
import com.ce.pms.charge.service.IChargeItemService;
import com.ce.pms.charge.vo.BusinessCodeVO;
import com.ce.pms.charge.vo.ChargeItemVO;
import com.ce.pms.common.model.SysrefcodeDO;
import com.ce.pms.customer.service.IConstService;
import com.ce.util.BlankUtil;
import com.sitechasia.webx.components.xtable.util.ExtremeUtils;
import com.sitechasia.webx.core.annotation.Read;
import com.sitechasia.webx.core.support.Page;

public class ChargeItemAction extends PmsBaseAction {
	private IConstService constService;
	private IBusinessCodeService businessCodeService;
	private IChargeItemService chargeItemService;

	public void setConstService(IConstService constService) {
		this.constService = constService;
	}



	public void setChargeItemService(IChargeItemService chargeItemService) {
		this.chargeItemService = chargeItemService;
	}



	public void setBusinessCodeService(IBusinessCodeService businessCodeService) {
		this.businessCodeService = businessCodeService;
	}

	/***
	 * 功能描述:  初始化收费项目及标准主界面<br>
	 * @return
	 * Create author:yandawei <br>
	 * Create on:2008-8-7  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public String initChargeItemMain()
	{
		return "chargeItemMain";
	}

	/***
	 * 功能描述: 收费项目列表 <br>
	 * @return
	 * Create author:yandawei <br>
	 * Create on:2008-8-7  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public String initChargeItemList(
			@Read(key = "ec_p", defaultValue = "1") Integer pageNO,
			@Read(key = "ec_crd",defaultValue = "15")Integer pageSize)
	{
		Page page = chargeItemService.queryChargeItemByPage(pageNO, pageSize,null);
		if(!BlankUtil.isBlank(page)){
			setAttribute("chargeItemList", page.getResult());
			setAttribute("totalRows",Integer.valueOf(String.valueOf(page.getTotalCount())));
		}else{
			setAttribute("totalRows",new Integer(0));
		}
		return "chargeItemList";
	}


	/***
	 * 功能描述:  跳转到新增收费项目界面<br>
	 * @return
	 * Create author:yandawei <br>
	 * Create on:2008-8-7  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public String initChargeItemAdd()
	{
		setAttribute("editFlag", "add");
		setAttribute("businessCodeList", getAvailableTradeCode(null));
		setAttribute("meterTypeList", getMeterType());
		setAttribute("isReference","0");
		ChargeItemVO chargeItem  = new ChargeItemVO();
		short seqNo = (short) (chargeItemService.getMaxSeqNo().intValue()+1);
		chargeItem.setSeqNo(seqNo);
		setAttribute("chargeItem",chargeItem);
		return "initChargeItem";
	}

	/**
	 * 功能描述: 新增收费项目 <br>
	 * @param chargeItem
	 * Create author:yandawei <br>
	 * Create on:2008-8-7  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public void addChargeItem(@Read ChargeItemVO chargeItem)
	{
		chargeItem.setCreator(getUserId());
		chargeItemService.addChargeItem(chargeItem);
		this.renderText("true");
	}

	/***
	 * 功能描述:  初始化修改收费项目界面<br>
	 * @param id
	 * @return
	 * Create author:yandawei <br>
	 * Create on:2008-8-7  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public String initChargeItemEdit(@Read(key = "keyword") Long id)
	{
		ChargeItemVO chargeItem = chargeItemService.findChargeItemById(id);
		setAttribute("editFlag", "update");
		if("0".equals(chargeItem.getReferSelect()))
			setAttribute("businessCodeList", getAvailableTradeCode(chargeItem.getBusinessCodeId().toString()));
		setAttribute("meterTypeList", getMeterType());
		setAttribute("chargeItem", chargeItem);
		setAttribute("isReference",chargeItem.getReferSelect());
		return "initChargeItem";
	}

	/***
	 * 功能描述:  修改收费项目<br>
	 * @param chargeItem
	 * Create author:yandawei <br>
	 * Create on:2008-8-7  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public void editChargeItem(@Read ChargeItemVO chargeItem)
	{
		chargeItem.setModifier(getUserId());
		chargeItemService.editChargeItem(chargeItem);
		this.renderText("true");
	}

	/***
	 * 功能描述: 删除收费项目 <br>
	 * @param id
	 * Create author:yandawei <br>
	 * Create on:2008-8-7  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public void deleteChargeItem(@Read(key = "keyword") Long id)
	{
		chargeItemService.deleteChargeItem(id);
		this.renderText("true");
	}


	/***
	 * 功能描述: 获取某个收费项目的详细信息 <br>
	 * @param id
	 * Create author:yandawei <br>
	 * Create on:2008-8-7  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public String getChargeItemDetailBy(@Read(key = "keyword") Long id)
	{
		ChargeItemVO chargeItem = chargeItemService.findChargeItemById(id);
		setAttribute("chargeItem", chargeItem);
		return "chargeItemDetail";
	}

	/***
	 * 功能描述:  刷新收费项目列表<br>
	 * @param pageNO
	 * @param pageSize
	 * @return
	 * Create author:yandawei <br>
	 * Create on:2008-8-7  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public String refreshChargeItemList(
			@Read(key = "ec_p", defaultValue = "1") Integer pageNO,
			@Read(key = "ec_crd",defaultValue = "15")Integer pageSize)
	{
		String sortStr=ExtremeUtils.getOrderString(this.getRequest());
		Page page = chargeItemService.queryChargeItemByPage(pageNO, pageSize,sortStr);
		setAttribute("chargeItemList", page.getResult());
		setAttribute("totalRows",Integer.valueOf(String.valueOf(page.getTotalCount())));
		return "chargeItemList";
	}

	/***
	 * 功能描述: 验证收费项目是否以存在 <br>
	 * @param name
	 * @param id
	 * Create author:yandawei <br>
	 * Create on:2008-8-27  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public void checkItemByName(@Read(key = "itemName") String name,
			@Read(key = "keyword") Long id)
	{
		String result = "true";
		boolean unique = chargeItemService.checkChargeItemNameIsOnly(name, id);
		if(unique){
			result = getLocaleMessage("charge:chargeItemExist");
		}
		renderText(result);
	}

	/***
	 * 功能描述: 验证序号是否唯一 <br>
	 * @param seqNo
	 * @param id
	 * Create author:yandawei <br>
	 * Create on:2008-8-27  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public void checkUniqueSeqNO(@Read(key = "seqNo") Short seqNo,
			@Read(key = "keyword") Long id)
	{
		String result = "true";
		boolean unique = chargeItemService.checkChargeItemSeqNoIsOnly(seqNo, id);
		if(unique){
			result = getLocaleMessage("charge:chargeItemSeqNoExist");
		}
		renderText(result);
	}

	/**
	 * 功能描述: 初始化收费项目树 <br>
	 *
	 * Create author:yandawei <br>
	 * Create on:2008-8-7  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public void initChargeItemTree()
	{
		HttpServletResponse response = getResponse();
		response.setContentType("text/html;charset=utf-8");
		response.setHeader("Cache-Control", "no-cache");
		try {
			PrintWriter out = response.getWriter();
			Element rootElement = new Element("tree");
			Document doc = new Document(rootElement);
			Attribute rootAttri = new Attribute("id", "0");
			rootElement.setAttribute(rootAttri);
			rootElement.setAttribute("select", "true");
			rootElement.setAttribute("tooltip", "根接点");

			Element item = new Element("item");
			item.setAttribute("text", "收费项目级及标准");
			item.setAttribute("id", "001");
			item.setAttribute("im1", "root.gif");
			item.setAttribute("im2", "root.gif");
			rootElement.addContent(item);

			Map map = chargeItemService.queryChargeItemAndStaTreeList();
			Set set = map.entrySet();
			for(Iterator iter = set.iterator() ; iter.hasNext() ;)
			{
				Map.Entry entry = (Entry) iter.next();
				List<ChargeItemVO> list = (List<ChargeItemVO>) entry.getValue();
				for(ChargeItemVO chargeItem : list){
					Element item1 = new Element("item");
					item1.setAttribute("id", chargeItem.getKeyword().toString()+":"+chargeItem.getChrgType());
					item1.setAttribute("text", chargeItem.getItemName());
					item1.setAttribute("im1", "folder.gif");
					item1.setAttribute("im2", "folder.gif");
					item.addContent(item1);
				}
			}
			XMLOutputter XMLOut = new XMLOutputter();
			XMLOut.output(doc, out);
			out.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 功能描述: 获取走表类型 <br>
	 * @return
	 * Create author:yandawei <br>
	 * Create on:2008-8-5  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	private List getMeterType()
	{
		return constService.getMeterType();
	}

	/***
	 * 功能描述: 获取所有可用的交易码(类型为收入类型且未被引用的) <br>
	 * @return
	 * Create author:yandawei <br>
	 * Create on:2008-8-5  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	private List getAvailableTradeCode(String exceptId)
	{
		List<BusinessCodeVO> list = businessCodeService.getDebitNotUseBusinessCodeList(exceptId);
		for(BusinessCodeVO businessCode : list){
			businessCode.setBizCode(buildingBusinessDisplay(businessCode.getBizCode(),businessCode.getDescription()));
		}
		return list;
	}

	/***
	 * 功能描述:  生成交易码+交易描述串<br>
	 * @param businessCode
	 * @param description
	 * @return
	 * Create author:yandawei <br>
	 * Create on:2008-11-19  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	private String buildingBusinessDisplay(String businessCode ,String description){
		StringBuffer sb = new StringBuffer();
		sb.append(businessCode).append("-").append(description);
		return sb.toString();
	}




}
